package com.au.bescene.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

import com.au.bescene.entity.Event;

/**
 * Utility class.
 * @author Khaled
 *
 */
public class Util {
	
	/**
	 * Parse list of events on server and create a sorted list of Event objects.
	 * @param listOfEventNames
	 * @return
	 */
	public static ArrayList<Event> parseToEvents(ArrayList<String> listOfEventNames) {
		ArrayList<Event> listOfEvents = new ArrayList<Event>();
		
		for(int i = 0; i < listOfEventNames.size(); i++) {
			listOfEvents.add(parseNameToEvent(listOfEventNames.get(i)));
		}

		sortEventListByDateDesc(listOfEvents);

		return listOfEvents;
	}
	
	/**
	 * Sorts list of Events descending by date.
	 * @param listOfEvents
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void sortEventListByDateDesc(ArrayList<Event> listOfEvents) {
		Collections.sort(listOfEvents, new Comparator() {
			public int compare(Object a, Object b) {
				Date dateA = ((Event) a).getDate();
				Date dateB = ((Event) b).getDate();
				return dateB.compareTo(dateA);
			}
		});
	}
	
	/**
	 * Sorts list of Events ascending by date.
	 * @param listOfEvents
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void sortEventListByDateAsc(ArrayList<Event> listOfEvents) {
		Collections.sort(listOfEvents, new Comparator() {
			public int compare(Object a, Object b) {
				Date dateA = ((Event) a).getDate();
				Date dateB = ((Event) b).getDate();
				return dateA.compareTo(dateB);
			}
		});
	}
	
	/**
	 * Parses the original folder name and splits it into name and date.
	 * @param name
	 * @return Event
	 */
	public static Event parseNameToEvent(String name) {
		 String[] splittedName = name.split("-");
		 
		 String nameOfEvent = splittedName[0].replace("_", " ");
		 
		 Date date = new Date(Integer.parseInt(splittedName[3]), Integer.parseInt(splittedName[2]), Integer.parseInt(splittedName[1]));
		 
		 return new Event(nameOfEvent, date, name);
	}
}
